package com.zjdiepu.www.manage.api.other;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.zjdiepu.www.base.BaseController;
import com.zjdiepu.www.base.annotation.Add;
import com.zjdiepu.www.base.annotation.Update;
import com.zjdiepu.www.base.core.AbstractSysUser;
import com.zjdiepu.www.base.core.ApiResponse;
import com.zjdiepu.www.base.core.PageVo;
import com.zjdiepu.www.base.enums.status.DpStatus;
import com.zjdiepu.www.base.enums.type.DataDicType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.common.util.CollectionInitUtil;
import com.zjdiepu.www.ibiz.cache.ICachedDictionaryService;
import com.zjdiepu.www.ibiz.other.IOtherMessageHandleService;
import com.zjdiepu.www.model.other.dto.OtherMessageHandle;
import com.zjdiepu.www.model.other.ext.OtherMessageHandleExt;

/**
 * Controller层<br/>
 * @author zhangpp<br/>
 * @time 2017年06月01日 18时07分<br/>
 * @since 1.0<br/>
 */
@RestController
@RequestMapping(value= "OtherMessageHandleController", produces="application/json;charset=UTF-8")
@Api(tags = {""}, produces="application/json;charset=UTF-8")
public class OtherMessageHandleController extends BaseController {
    @Resource
    private IOtherMessageHandleService otherMessageHandleService;
    @Resource
    private ICachedDictionaryService cachedDictionaryService;

    /**
     * @author zhangpp <br/>
     * @Title: save  <br/>
     * @Description: 新增 <br/>
     * @param otherMessageHandle
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "新增", response = Boolean.class, httpMethod="POST", tags={"新增"})
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public ApiResponse save(@Validated(Add.class) @ModelAttribute OtherMessageHandle otherMessageHandle) {
    	OtherMessageHandleExt otherMessageHandleExt=new OtherMessageHandleExt();
    	otherMessageHandleExt.notDeleted();
    	otherMessageHandleExt.withOutPage();
    	otherMessageHandleExt.setTypeMsgEvent(otherMessageHandle.getTypeMsgEvent());
    	List<OtherMessageHandle> list=otherMessageHandleService.selectByExt(otherMessageHandleExt);
    	if(list !=null && list.size()>0){
    		return ApiResponse.fail(ResultFlagType.REPEAT_DATA);
    	}
    	
    	AbstractSysUser currentUser = getCurrentUser();
    	otherMessageHandle.setModifier(currentUser.getId()); //统一使用修改人和修改人名称来初始化
    	otherMessageHandle.setModifierName(currentUser.getName());
    	
    	otherMessageHandleService.saveAll(currentUser,otherMessageHandle);
        return ApiResponse.success(true);
    }

    /**
     * @author zhangpp <br/>
     * @Title: remove  <br/>
     * @Description: 删除<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "删除", response = Boolean.class, httpMethod="POST", tags={"删除"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "remove", method = RequestMethod.POST)
    public ApiResponse remove(String id) {
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	AbstractSysUser currentUser = getCurrentUser();
    	List<String> ids = Arrays.asList(id.split(","));
    	otherMessageHandleService.updateBatch(currentUser.getId(), currentUser.getName(), DpStatus.DELETED.getCode(), ids);
        return ApiResponse.success(true);
    }
    
    /**
     * @author zhangpp <br/>
     * @Title: disable  <br/>
     * @Description: 禁用<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "禁用", response = Boolean.class, httpMethod="POST", tags={"禁用"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "disable", method = RequestMethod.POST)
    public ApiResponse disable(String id) {
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	AbstractSysUser currentUser = getCurrentUser();
    	List<String> ids = Arrays.asList(id.split(","));
    	otherMessageHandleService.updateBatch(currentUser.getId(), currentUser.getName(), DpStatus.INVALID.getCode(), ids);
        return ApiResponse.success(true);
    }
    
    /**
     * @author zhangpp <br/>
     * @Title: enable <br/>
     * @Description: 启用<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "启用", response = Boolean.class, httpMethod="POST", tags={"启用"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "enable", method = RequestMethod.POST)
    public ApiResponse enable(String id) {
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	AbstractSysUser currentUser = getCurrentUser();
    	List<String> ids = Arrays.asList(id.split(","));
    	otherMessageHandleService.updateBatch(currentUser.getId(), currentUser.getName(), DpStatus.NORMAL.getCode(), ids);
        return ApiResponse.success(true);
    }

    /**
     * @author zhangpp <br/>
     * @Title: modify  <br/>
     * @Description: 修改 <br/>
     * @param otherMessageHandle
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "修改", response = Boolean.class, httpMethod="POST", tags={"修改"})
    @RequestMapping(value = "modify", method = RequestMethod.POST)
    public ApiResponse modify(@Validated(Update.class) @ModelAttribute OtherMessageHandle otherMessageHandle) {
    	AbstractSysUser currentUser = getCurrentUser();
    	otherMessageHandle.setModifier(currentUser.getId()); //统一使用修改人和修改人名称来初始化
    	otherMessageHandle.setModifierName(currentUser.getName());
    	otherMessageHandleService.modifyAll(currentUser,otherMessageHandle);
        return ApiResponse.success(true);
    }

    /**
     * @author zhangpp <br/>
     * @Title: getById  <br/>
     * @Description: 主键查询<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "主键查询", response = OtherMessageHandle.class, httpMethod="POST", tags={"主键查询"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "getById", method = RequestMethod.POST)
    public ApiResponse getById(String id){
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
       OtherMessageHandle otherMessageHandle = otherMessageHandleService.selectById(id);
       if(null == otherMessageHandle) {
    	   return ApiResponse.fail(ResultFlagType.MISSING);
       }
       return ApiResponse.success(otherMessageHandle);
    }
    
    /**
     * @author zhangpp <br/>
     * @Title: list  <br/>
     * @Description: 条件查询 <br/>
     * @param otherMessageHandleExt
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "条件查询",response = OtherMessageHandle.class, httpMethod="POST", tags={"列表"})
    @RequestMapping(value = "list", method = RequestMethod.POST)
    public ApiResponse list(@ModelAttribute OtherMessageHandleExt otherMessageHandleExt){
        otherMessageHandleExt.notDeleted();
        PageVo page = getPage(otherMessageHandleExt);
        int records = otherMessageHandleService.countByExt(otherMessageHandleExt);
        page.setRecords(records);
        if (records > 0) {
            List<OtherMessageHandle> otherMessageHandles = otherMessageHandleService.selectByExt(otherMessageHandleExt);
            for (OtherMessageHandle otherMessageHandle : otherMessageHandles) {
            	otherMessageHandle.setStatusMsgHandleDisplay(cachedDictionaryService.getName(otherMessageHandle.getStatusMsgHandle(),DataDicType.STATUS_MSG_HANDLE));
            	otherMessageHandle.setTypeMsgEventDisplay(cachedDictionaryService.getName(otherMessageHandle.getTypeMsgEvent(),DataDicType.MESSAGE_EVENT_TYPE));
			}
            page.setRows(otherMessageHandles);
        } else {
            page.setRows(CollectionInitUtil.emptyList());
        }
        return ApiResponse.success(page);
    }
    
    @ApiOperation(value = "多表查询",response = OtherMessageHandle.class, httpMethod="POST", tags={"列表"})
    @RequestMapping(value = "selectMulitList", method = RequestMethod.POST)
    public ApiResponse selectMulitList(@ModelAttribute OtherMessageHandleExt otherMessageHandleExt){
        otherMessageHandleExt.notDeleted();
        PageVo page = getPage(otherMessageHandleExt);
        otherMessageHandleExt.setValueMsgHandle(1);
        int records = otherMessageHandleService.countMulitByExt(otherMessageHandleExt);
        page.setRecords(records);
        String msg="";
        String statusMsg="";
        String[] statusMsgs=null;
        if (records > 0) {
            List<OtherMessageHandle> otherMessageHandles = otherMessageHandleService.selectMulitByExt(otherMessageHandleExt);
            for (int i=0; i< otherMessageHandles.size(); i++) {
            	OtherMessageHandle otherMessageHandle=otherMessageHandles.get(i);
            	statusMsg=otherMessageHandle.getStatusMsgHandles();
            	if(otherMessageHandle.getStatusMsgHandleDisplay()!=null &&otherMessageHandle.getStatusMsgHandleDisplay().contains(",")){
            		otherMessageHandle.setStatusMsgHandleDisplay(otherMessageHandle.getStatusMsgHandleDisplay().substring(0,otherMessageHandle.getStatusMsgHandleDisplay().length()-1));
            	}
            	if(statusMsg!=null && statusMsg.contains(",")){
            		statusMsgs=statusMsg.split(",");
        			for(int j=0;j<statusMsgs.length;j++){
            			msg+=cachedDictionaryService.getName(statusMsgs[j],DataDicType.STATUS_MSG_HANDLE)+",";
					}
            		otherMessageHandle.setStatusMsgHandleDisplay(msg.substring(0,msg.length()-1));
            		msg="";
            	}
            	else{
            		otherMessageHandle.setStatusMsgHandleDisplay(cachedDictionaryService.getName(statusMsg,DataDicType.STATUS_MSG_HANDLE));
            	}
            	otherMessageHandle=null;
			}
            page.setRows(otherMessageHandles);
        } else {
            page.setRows(CollectionInitUtil.emptyList());
        }
        return ApiResponse.success(page);
    }

}
